import type { NextApiResponse } from 'next';
import * as newsService from '@/common/service/g_news';
import R from '@common/type/response';
import { AuthenticatedRequest, withAuth } from '@common/middleware/auth';

export async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
  if (req.method === 'GET') {
    const { _start = 0, _end = 10 } = req.query;
    const data = await newsService.getNewsList({}, Number(_start), Number(_end));
    R.success(res,data)
  } else if (req.method === 'POST') {
    try {
      const id = await newsService.createNews(req.body);
      R.success(res, id);
    } catch (error: any) {
      console.error('创建新闻错误:', error);
      R.error(res, error.message || '创建新闻失败');
    }
  } else {
    R.error(res, 'Method Not Allowed');
  }
}

export default withAuth(handler); 